package com.bw.zg3.Log

import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.mvvm.BaseActivity
import com.bw.zg3.databinding.ActivityLogBinding
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/app/LogActivity")
@AndroidEntryPoint
class LogActivity : BaseActivity<ActivityLogBinding, UserViewModel>(){


    override fun initView() {
//        val long = SPUtils.getInstance().getLong("LoginTime", 0)
//        val currenTime=System.currentTimeMillis()
//        val d = (currenTime - long) / 1000 / 60 / 60/24
//        if (d <7) {
//            start1()
//        }
        binding.btnLogin.setOnClickListener {
            val name = binding.loginUname.text.toString()
            val pwd = binding.loginPass.text.toString()
            if (name.isEmpty()||pwd.isEmpty()){
                ToastUtils.showLong("输入内容不能为空")
                return@setOnClickListener
            }
//            if(!name.matches(Regex("[a-zA-Z0-9]{2,8}"))){
//                ToastUtils.showLong("用户名格式不正确")
//                return@setOnClickListener
//            }
            if (!RegexUtils.isMobileSimple(name)){
                ToastUtils.showLong("用户名格式不正确")
          return@setOnClickListener
            }
            vm.getLog(name,pwd)


        }
        //去注册账号
        binding.loginRegister.setOnClickListener {
            ARouter.getInstance().build("/app/ZCActivity").navigation()

        }
    }

    override fun intiData() {
        vm.LoginData.observe(this){
            SPUtils.getInstance().apply {
                put("token",it.token)
                put("username",it.username)
                put("userid",it.id)
               put("Rote",it.roleid)
                put("LoginTime",System.currentTimeMillis())
            }
//            start1()

            ARouter.getInstance().build("/app/MainActivity").navigation()
        }
    }
//    fun start1(){
//        ARouter.getInstance().build("/app/MainActivity").navigation()
//        finish()
//    }

}